package com.gw.smscheck.controller.business.focuscrowd;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gw.smscheck.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

import com.gw.smscheck.util.ResultObj;
import com.gw.smscheck.util.ResultConversion;
import io.swagger.annotations.Api;
import springfox.documentation.annotations.ApiIgnore;
import io.swagger.annotations.ApiOperation;
import com.gw.smscheck.entity.business.focuscrowd.FocusCrowd;
import com.gw.smscheck.service.business.focuscrowd.FocusCrowdService;
/**
 * 重点人员管理
 * 
 * @author pzr
 * @date 2018年10月29日
 */
@Api(description="【重点人员】")
@Controller
@RequestMapping("/focusCrowdController")
public class FocusCrowdController {
	@Autowired
	private FocusCrowdService focusCrowdService;

	/**
	 * 跳转到编辑页面
	 * 
	 * @return
	 */
	@RequestMapping("/goEdit")
	@ApiIgnore
	public ModelAndView goEdit( FocusCrowd focusCrowd) {
		String id = "";
		if (focusCrowd.getId() != null){
			id = focusCrowd.getId().toString();
		}
		ModelAndView modelAndView = new ModelAndView("business/views/url/focuscrowdedit");
		modelAndView.addObject("id", id);
		return modelAndView;
	}
	/**
	 * 添加
	 * @param response
	 * @param focusCrowd
	 * @return
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value="添加", notes="添加")
	public String add(HttpServletRequest request,HttpServletResponse response, FocusCrowd focusCrowd) {
		//User user = (User) request.getSession().getAttribute("user");
		//focusCrowd.setCreateUser(String.valueOf(user.getId()));
		return ResultObj.successResultObj(focusCrowdService.add(focusCrowd));
	}
	/**
	 * 查询所有
	 * 
	 * @return
	 */
	@RequestMapping(value = "/findAll", method = { RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value="查询所有", notes="查询所有")
	public String findAll() {
		List<FocusCrowd> list = focusCrowdService.findAll();
		return JSON.toJSONString(list);
	}
	/**
	 * 分页查询
	 * @param focusCrowd
	 * @return
	 */
	@RequestMapping(value = "/pageSelect", method = { RequestMethod.POST })
	@ResponseBody
	@ApiOperation(value="分页查询", notes="分页查询")
	public String pageSelect(FocusCrowd focusCrowd) {
		PageInfo<FocusCrowd> pageInfo = focusCrowdService.pageSelect(focusCrowd);
		return ResultConversion.ToEasyUIDataGrid(pageInfo).toJSONString();
	}
	/**
	 * 通过id获取
	 * @param dto
	 * @return
	 */
	@RequestMapping(value = "/findById", method = { RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value="通过id获取", notes="通过id获取")
	public String findById(FocusCrowd dto){
		return ResultObj.successResultObj(focusCrowdService.findById(dto.getId()));
	}
	/**
	 * 修改
	 * @param focusCrowd
	 * @return
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value="修改", notes="修改")
	public String update(FocusCrowd focusCrowd) {
		return ResultObj.successResultObj(focusCrowdService.update(focusCrowd));
	}
	/**
	 * 批量删除
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/delByIds", method = { RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value="批量删除", notes="批量删除")
	public String delByIds(Integer id){
		return ResultObj.successResultObj(focusCrowdService.delByIds(id));
	}

	/**
	 * 根据多个编号返回多个详情
	 * @param codes
	 * @return
	 */
	@RequestMapping(value = "/findByCodes", method = { RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value="多编号查询", notes="多编号查询")
	public String findByCodes(String[] codes){
		return ResultObj.successResultObj(focusCrowdService.findByCodes(codes));
	}

	/**
	 * 批量上传打印在页面上
	 * @param file
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="批量上传", notes="批量上传")
	public String upload(@RequestParam(value = "excelFile", required = false) MultipartFile file, HttpServletRequest request) {
		try {
			MultipartRequest multipartRequest=(MultipartRequest) request;
			MultipartFile excelFile=multipartRequest.getFile("excelFile");
			if(excelFile!=null){
				List<List<String>> datas = ExcelUtil.readXlsx(excelFile.getInputStream());
				//TODO: 读到的数据都在datas里面，根据实际业务逻辑做相应处理<br>                // .............
				if(datas!=null && datas.size()>0){
					FocusCrowd fc = null;
					List<FocusCrowd> resultList = new ArrayList<>();
					for(int i = 0;i < datas.size();i ++){
						fc = new FocusCrowd();
						fc.setPhoneNum(datas.get(i).get(0)); // 电话号码
						fc.setNoticePeople(datas.get(i).get(1)); // 联系人
						fc.setNoticePhone(datas.get(i).get(2)); // 联系人电话
						resultList.add(fc);
					}
					return ResultObj.successResultObj(resultList);
				}
			}else{
				return ResultObj.failResultObj(null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.failResultObj(null);
		}
		return ResultObj.successResultObj(null);
	}

	/**
	 * 批量保存
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/excelSave", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value="批量保存", notes="批量保存")
	public String excelSave(FocusCrowd dto){
		JSONArray jsonArray = JSONArray.parseArray(dto.getFocusCrowdList());
        for (Object jsonObject : jsonArray) {
            FocusCrowd focusCrowd = JSONObject.parseObject(jsonObject.toString(),FocusCrowd.class);
			focusCrowd.setEnterAlarmValue(dto.getEnterAlarmValue());
			focusCrowd.setEnterAlarmValueIds(dto.getEnterAlarmValueIds());
			focusCrowd.setLeaveAlarmValue(dto.getLeaveAlarmValue());
			focusCrowd.setLeaveAlarmValueIds(dto.getLeaveAlarmValueIds());
			focusCrowd.setTimeType(dto.getTimeType());// 时间类型
			if(dto.getTimeType() == 1){ // 天
				focusCrowd.setDays(dto.getDays());
			}else if(dto.getTimeType() == 3){ // 自定义
				focusCrowd.setTimeStart(dto.getTimeStart());
				focusCrowd.setTimeEnd(dto.getTimeEnd());
			}
			focusCrowd.setType(dto.getType());// 人员类型
			Integer i = focusCrowdService.add(focusCrowd);
			if(i == null){
				return ResultObj.failResultObj(null);
			}
        }
		return ResultObj.successResultObj(dto);
	}
}

